<%@ page import="entity.Violation_record" %>
<%@ page import="java.util.List" %>
<%@ page import="com.mysql.cj.util.StringUtils" %>
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>违纪记录管理</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: 'Helvetica Neue', Arial, sans-serif;
            background: rgb(255, 255, 255);
            color: #333;
        }
        .main-content {
            width: 100%;
            max-width: 1200px;
            margin: 0 auto;
            padding: 24px;
            background: #fff;
            border-radius: 6px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
        }
        .action-bar {
            display: flex;
            gap: 12px;
            margin-bottom: 24px;
            align-items: center;
        }
        input {
            padding: 10px 14px;
            border: 1px solid #ced4da;
            border-radius: 4px;
            width: 200px;
            font-size: 14px;
            transition: border-color 0.15s;
        }
        input:focus {
            border-color: #4dabf7;
            outline: none;
            box-shadow: 0 0 0 3px rgba(77, 171, 247, 0.1);
        }
        button {
            padding: 10px 18px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            font-weight: 500;
            transition: all 0.2s;
        }
        .btn-search {
            background: #1a73e8;
            color: white;
        }
        .btn-search:hover {
            background: #1765cc;
        }
        .btn-new {
            background: #34a853;
            color: white;
        }
        .btn-new:hover {
            background: #2d9249;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            background: white;
            border: 1px solid #e0e0e0;
            margin-top: 16px;
            font-size: 14px;
        }
        th, td {
            padding: 14px 16px;
            text-align: left;
            border-bottom: 1px solid #e0e0e0;
        }
        th {
            background: #f5f7fa;
            color: #4a5568;
            font-weight: 600;
            text-transform: uppercase;
            font-size: 13px;
            letter-spacing: 0.5px;
        }
        tr:hover {
            background-color: #f8fafc;
        }
        .action-btns button {
            padding: 6px 12px;
            margin-right: 8px;
            font-size: 13px;
        }
        .btn-edit {
            background: #f6ad55;
            color: white;
        }
        .btn-edit:hover {
            background: #e69a3e;
        }
        .btn-del {
            background: #f56565;
            color: white;
        }
        .btn-del:hover {
            background: #e53e3e;
        }
        .pagination {
            margin-top: 24px;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 12px;
        }
        .btn-prev, .btn-next {
            padding: 8px 16px;
            background: #edf2f7;
            color: #4a5568;
        }
        .btn-prev:hover, .btn-next:hover {
            background: #e2e8f0;
        }
        .btn-prev:disabled, .btn-next:disabled {
            background: #edf2f7;
            color: #a0aec0;
            cursor: not-allowed;
        }
        .badge-warning {
            background: #f6ad55;
            padding: 4px 8px;
            border-radius: 4px;
            color: white;
            font-size: 12px;
            font-weight: 500;
        }
        .badge-danger {
            background: #f56565;
            padding: 4px 8px;
            border-radius: 4px;
            color: white;
            font-size: 12px;
            font-weight: 500;
        }
        .modal-title {
            font-size: 18px;
            font-weight: 600;
            color: #2d3748;
            margin-bottom: 20px;
        }
        .form-group {
            margin-bottom: 16px;
        }
        .form-label {
            display: block;
            margin-bottom: 6px;
            font-size: 14px;
            color: #4a5568;
            font-weight: 500;
        }
    </style>
</head>
<body>
<div class="main-content">
    <h3 style="margin-bottom:16px;color:rgba(0,0,0,0.85)">违纪记录管理</h3>
    <div class="action-bar">
        <input placeholder="学号" name="searchKey" id="searchKey">
        <button class="btn-search" onclick="loadViolations()">查询</button>
        <button class="btn-new" onclick="openAddModal()">新增记录</button>
    </div>
    <%--    <%--%>
    <%--        List<Violation_record> list = (List<Violation_record>)request.getAttribute("list");--%>
    <%--        Integer currentPage = (Integer) request.getAttribute("currentPage");--%>
    <%--        Boolean hasNextPage = (Boolean) request.getAttribute("hasNextPage");--%>


    <%--    %>--%>

    <table id="violationTable">
        <thead>
        <tr>
            <th>学号</th>
            <th>姓名</th>
            <th>违纪类型</th>
            <th>处分措施</th>
            <th>记录时间</th>
            <th>审核人</th>
            <th>审核状态</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>

        </tbody>
    </table>
    <!-- 分页控件 -->
    <div class="pagination">
        <button id="prevPage" class="btn-prev" onclick="prevPage()">上一页</button>
        <span id="pageInfo">第1页</span>
        <button id="nextPage" class="btn-next" onclick="nextPage()">下一页</button>
    </div>

</div>

<!-- 新增记录模态框 -->
<div id="addModal" style="display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); z-index: 999;">
    <div style="position: relative; background: white; width: 500px; margin: 100px auto; padding: 24px; border-radius: 8px; box-shadow: 0 4px 12px rgba(0,0,0,0.15);">
        <h3 class="modal-title">新增违纪记录</h3>
        <div class="form-group">
            <label class="form-label">姓名：</label>
            <input type="text" id="student_name"  style="width: 100%; padding: 10px; border: 1px solid #e2e8f0; border-radius: 4px;">
        </div>
        <div class="form-group">
            <label class="form-label">违纪类型：</label>
            <select name="violation_type" id="violation_type1" style="width: 100%; padding: 10px; border: 1px solid #e2e8f0; border-radius: 4px;">
                <option value="旷课">旷课</option>
                <option value="晚归">晚归</option>
                <option value="作弊">作弊</option>
                <option value="打架">打架</option>
                <option value="其他">其他</option>
            </select>
        </div>
        <div class="form-group">
            <label class="form-label">处分措施：</label>
            <input type="text" name="punishment" id="punishment1" required style="width: 100%; padding: 10px; border: 1px solid #e2e8f0; border-radius: 4px;">
        </div>
        <div style="margin-bottom:15px;">
            <label>记录时间：</label>
            <input type="date" id="record_time1" style="width:100%;padding:8px;" name="record_time">
        </div>
        <div style="margin-bottom:15px;">
            <label>审核人：</label>
            <input type="text" id="auditor1" style="width:100%;padding:8px;" name="auditor">
        </div>
        <div style="margin-bottom:15px;">
            <label>审核状态：</label>
            <select name="text" id="status1" style="width:100%;padding:8px;" name="status">
                <option value="待审核">待审核</option>
                <option value="已生效">已生效</option>
            </select>
        </div>

        <div style="text-align: right; margin-top: 24px;">
            <button type="button" onclick="closeAddModal()" style="margin-right: 10px; background: #edf2f7; color: #4a5568;">取消</button>
            <button type="button" onclick="submitAddForm()" style="background: #1a73e8; color: white;">提交</button>
        </div>
    </div>
</div>

<!-- 编辑记录模态框 -->
<div id="editModal" style="display:none;position:fixed;top:0;left:0;width:100%;height:100%;background:rgba(0,0,0,0.5);z-index:1000;">
    <input type="hidden" id="aditId">
    <div style="position:relative;width:400px;margin:100px auto;padding:20px;background:#fff;border-radius:8px;">
        <h3 style="margin-bottom:20px;">编辑违纪记录</h3>
        <div style="margin-bottom:15px;">
            <label>违纪类型：</label>
            <select id="violation_type" name="violation_type" style="width:100%;padding:8px;">
                <option value="旷课">旷课</option>
                <option value="晚归">晚归</option>
                <option value="作弊">作弊</option>
                <option value="打架">打架</option>
                <option value="其他">其他</option>
            </select>
        </div>
        <div style="margin-bottom:15px;">
            <label>处分措施：</label>
            <input type="text" id="punishment" name="punishment" style="width:100%;padding:8px;">
        </div>
        <div style="margin-bottom:15px;">
            <label>记录时间：</label>
            <input type="text" id="record_time" name="record_time" style="width:100%;padding:8px;">
        </div>
        <div style="margin-bottom:15px;">
            <label>审核人：</label>
            <input type="text" id="auditor" name="auditor" style="width:100%;padding:8px;">
        </div>
        <div style="margin-bottom:15px;">
            <label>审核状态：</label>
            <select id="status" name="status" style="width:100%;padding:8px;">
                <option value="待审核">待审核</option>
                <option value="已生效">已生效</option>
            </select>
        </div>

        <div style="text-align:right;">
            <button type="button" onclick="closeEditModal()" style="margin-right:10px;">取消</button>
            <button type="button" onclick="submitEditForm()" style="background:#1890ff;color:white;">提交</button>
        </div>
    </div>
</div>

<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>


    //页面加载时获取用户数据
    $(document).ready(function () {
        loadViolations();
    });


    // 渲染表格数据
    function renderTable(data) {
        let html = "";

        if (data && data.length > 0) {
            $.each(data, function(index, record) {
                html += '<tr>' +
                    '<td>' + (record.student_id || '') + '</td>' +
                    '<td>' + (record.student_name || '') + '</td>' +
                    '<td>' + (record.violation_type || '') + '</td>' +
                    '<td>' + (record.punishment || '') + '</td>' +
                    '<td>' + (record.record_time || '') + '</td>' +
                    '<td>' + (record.auditor || '') + '</td>' +
                    '<td>' + (record.status || '') + '</td>' +
                    '<td class="action-btns">' +
                    '<button class="btn-edit" onclick="openEditModal(\'' + record.id + '\',\'' + record.violation_type + '\',\'' + record.punishment + '\',\''+record.record_time+'\',\''+record.auditor+'\',\''+record.status+'\')">修改</button>' +
                    '<button class="btn-del" onclick="deleteViolation(\'' + record.id + '\')">删除</button>' +
                    '</td>' +
                    '</tr>';
            });
        } else {
            html = '<tr><td colspan="6" style="text-align:center;">暂无数据</td></tr>';
        }
        $("#violationTable tbody").html(html);
    }
    // 添加分页变量定义
    let currentPage = 0;
    let totalPages = 0;
    let totalCount = 0;
    const pageSize = 5;

    // 打开新增模态框
    function openAddModal() {
        document.getElementById("addModal").style.display = "block";
    }


    // 提交新增表单
    // 修改submitAddForm函数
    function submitAddForm() {
        const formData = {
            student_name: $('#student_name').val(),
            violation_type: $('#violation_type1').val(),
            punishment: $('#punishment1').val(),
            record_time: $('#record_time1').val(),
            auditor: $('#auditor1').val(),
            status: $('#status1').val()
        };
        $.ajax({
            url: '<%=request.getContextPath()%>/violation_recordController',
            type: 'POST',
            data: {
                op: "INSERT",
                student_name: formData.student_name,
                violation_type: formData.violation_type,
                punishment: formData.punishment,
                record_time: formData.record_time,
                auditor: formData.auditor,
                status: formData.status
            },
            dataType: 'json',
            success: function(response) {
    
                    if (typeof response === 'string') {
                        response = JSON.parse(response);
                    }
                    if (response && response.success) {
                        alert(response.message || '添加成功');
                        closeAddModal();
                        loadViolations();
                    } else {
                        alert(response.message || '添加失败');
                    }
        
            },
        });
    }








    // 关闭新增模态框
    function closeAddModal() {
        document.getElementById("addModal").style.display = "none";
    }
    // 打开编辑模态框
    function openEditModal(id, violationType, punishment, record_time, auditor, status) {
        $('#aditId').val(id);
        $('#violation_type1').val(violationType);
        $('#punishment').val(punishment);
        $('#record_time').val(record_time);
        $('#auditor').val(auditor);
        $('#status').val(status);
        $('#editModal').show();
    }

    // 提交编辑表单
    // 修改后的submitEditForm函数
    function submitEditForm() {
        const formData = {
            op: "update",
            id: $('#aditId').val(),
            violation_type: $('#violation_type').val(),
            punishment: $('#punishment').val(),
            record_time: $('#record_time').val(),
            auditor: $('#auditor').val(),
            status: $('#status').val()
        };
    
        // 添加必填字段验证
        if (!formData.violation_type || !formData.punishment) {
            alert('违纪类型和处分措施不能为空');
            return;
        }
    
        $.ajax({
            url: '<%=request.getContextPath()%>/violation_recordController',
            type: 'POST',
            data: formData,
            dataType: 'json',
            success: function(response) {
                if (response && response.success) {
                    alert(response.message || '修改成功');
                    loadViolations(); // 确保在修改成功后调用
                    closeEditModal(); // 先刷新再关闭模态框
                } else {
                    alert(response.message || '修改失败');
                    loadViolations();
                }
            }
        });
    }

    // 关闭编辑模态框
    function closeEditModal() {
        $('#editModal').hide();
    }
    // 分页控制函数
    function prevPage() {
        if (currentPage > 0) {
            currentPage--;
            loadViolations();
        }
    }

    function nextPage() {
        if (currentPage < totalPages - 1) {
            currentPage++;
            loadViolations();
        }
    }

    // 修改后的loadViolations函数
    function loadViolations() {
        const searchKey = $("#searchKey").val();

        $.ajax({
            url: '<%=request.getContextPath()%>/violation_recordController',
            type: 'POST',
            data: {
                op: "Violation",
                searchKey: searchKey,
                page: currentPage,
                pageSize: pageSize
            },
            dataType: 'json',
            success: function(response) {
                if (response && response.data) {
                    renderTable(response.data);

                    // 更新分页信息
                    currentPage = response.currentPage || 0;
                    totalPages = response.totalPages || 0;
                    totalCount = response.totalCount || 0;

                    // 更新分页控件显示
                    $("#pageInfo").text("第" + (currentPage + 1) + "页/共" + totalPages + "页");

                    // 更新按钮状态
                    $("#prevPage").prop("disabled", currentPage <= 0);
                    $("#nextPage").prop("disabled", currentPage >= totalPages - 1);
                }
            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.error("查询失败:", textStatus, errorThrown);
                alert("请输入ID");
            }
        });
    }



    // 修改后的deleteViolation函数
    function deleteViolation(id){
        if (!id || isNaN(id)){
            alert('无效的ID');
            return;
        }
        
        if (!confirm("确定要删除这条违纪记录吗？删除后将无法恢复！")) {
            return;
        }
        
        $.ajax({
            url: '<%=request.getContextPath()%>/violation_recordController',
            type: 'POST',
            data: {
                op: 'delete',
                id: id
            },
            dataType: 'json',
            success: function(response) {
                if (response && response.success) {
                    alert(response.message || '删除成功');
                    loadViolations(); // 刷新列表
                } else {
                    alert(response.message || '删除失败');
                }
            },
            error: function(xhr) {
                try {
                    const errResponse = JSON.parse(xhr.responseText);
                    alert(errResponse.message || '删除请求失败');
                } catch(e) {
                    alert('删除请求失败: ' + xhr.statusText);
                }
            }
        });
    }
</script>
</body>
</html>
